package com.hotstrip.common.excptions;

import com.hotstrip.model.R;
import com.hotstrip.model.enums.ResponseCodeEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @description 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * @description 自定义异常处理
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object exceptionHandler(HttpServletRequest req, Exception e) throws Exception {
        logger.error("异常url: {}", req.getRequestURL().toString());
        logger.error("错误信息......message: {}......cause: {}", e.getMessage(), e.getCause());
        return R.error(ResponseCodeEnums.ERROR.getValue(), e.getMessage());
    }


    /**
     * @description 自定义异常处理
     * 平安接口对接  自定义异常
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public Object myExceptionHandler(HttpServletRequest req, MyException e) throws Exception {
        logger.error("异常url: {}", req.getRequestURL().toString());
        logger.error("myException错误信息......message: {}......cause: {}", e.getMessage(), e.getCause());
        return R.error(ResponseCodeEnums.ERROR.getValue(), "操作异常");
    }

}

